package com.ds.infrastructure.hyperspace.console.model.request.deploy;

import com.ds.infrastructure.hyperspace.console.dto.deploydata.DeployDataDTO;
import com.ds.infrastructure.hyperspace.console.entity.table.TbDeployConfig;
import com.ds.infrastructure.hyperspace.console.exceptions.HyperspaceException;
import com.ds.infrastructure.hyperspace.console.model.request.deploy.deploydata.DeployDataForm;
import com.ds.infrastructure.hyperspace.console.utils.FastJsonUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * @author: yaozhendong
 * @create: 2019-10-21 10:38
 **/
@Data
@NoArgsConstructor
public class SaveDeployForm {
    @NotNull
    private Long id;
    @ApiModelProperty(example = "2018-10-01 12:18:48")
    private Date gmtModified;
    private Integer configVersion;
    private DeployDataForm container;

    public static TbDeployConfig toEntity(SaveDeployForm form) {
        TbDeployConfig entity = new TbDeployConfig();
        entity.setId(form.getId());
        entity.setGmtModified(form.getGmtModified());
        DeployDataDTO deployDataDTO = SaveDeployForm.toDTO(form);
        entity.setConfigData(FastJsonUtil.serialize(deployDataDTO));
        return entity;
    }

    private static DeployDataDTO toDTO(SaveDeployForm form) {
        DeployDataDTO dto = new DeployDataDTO();
        if (form.getContainer() == null) {
            return dto;
        }
        return DeployDataForm.toDTO(form.getContainer());
    }

    public static SaveDeployForm toVO(TbDeployConfig entity) {
        SaveDeployForm form = new SaveDeployForm();
        form.setId(entity.getId());
        form.setGmtModified(entity.getGmtModified());
        form.setConfigVersion(entity.getConfigVersion());
        addDeployDataForm(entity, form);
        return form;

    }

    private static void addDeployDataForm(TbDeployConfig entity, SaveDeployForm form) {
        if (StringUtils.isBlank(entity.getConfigData())) {
            return;
        }
        DeployDataDTO deployDataDTO = FastJsonUtil.deserialize(entity.getConfigData(), DeployDataDTO.class);
        if (deployDataDTO == null) {
            return;
        }
        DeployDataForm deployDataForm = DeployDataForm.toVO(deployDataDTO);
        form.setContainer(deployDataForm);
    }

    public static void checkConfigData(TbDeployConfig entity) {
        SaveDeployForm form = toVO(entity);
        DeployDataForm container = form.getContainer();
        if (container == null) {
            throw new HyperspaceException("部署配置数据为空");
        }
        if (container.getPackageInfo() == null) {
            throw new HyperspaceException("PackageInfo为空");
        }
        if (container.getHostGroups() == null || container.getHostGroups().isEmpty()) {
            throw new HyperspaceException("HostGroup集合数据为空");
        }
        container.getHostGroups().forEach(item -> {
            if (item.getHosts() == null || item.getHosts().isEmpty()) {
                throw new HyperspaceException(String.format("HostGroupName=%s,Host集合数据为空", item.getGroupName()));
            }
        });
    }
}
